<?php
/**
 * Name: AdminLogLogic,class.php
 * Description:
 * User: DIY
 * Date: 2016/9/22
 * Time: 17:39
 */

namespace Common\Logic;


use Admin\Library\BootstrapPage;
use Common\Model\AdminLogModel;

class AdminLogLogic
{
    private $AdminLogModel;

    public function __construct()
    {
        $this->AdminLogModel = new AdminLogModel();
    }

    /*
     *
     * 获取日志列表
     * */
    public function getAdminLogList($data=array()){
        $param['1'] = '1';
        if($data['username']!=''){
            $param['p2.username'] = array('like','%'.$data['username'].'%');
        }
        if($data['route']!=''){
            $param['p1.route'] = array('like','%'.$data['route'].'%');
        }
        //日期转换
        if($data['start_date']!=''){
            $param['p1.add_time'][] = array('gt',strtotime($data['start_date']));
        }
        if($data['end_date']!=''){
            $param['p1.add_time'][] = array('lt',strtotime($data['end_date']));
        }

        $count = $this->AdminLogModel
            ->alias('p1')
            ->join('LEFT JOIN __ADMIN_USER__ as p2 on p1.admin_id=p2.id')
            ->join('LEFT JOIN __ADMIN_AUTH__ as p3 on p1.route=p3.url')
            ->where($param)
            ->count();
        $Page = new BootstrapPage($count,20);

        $list['page'] = $Page->show();// 分页显示输出

        $list['list'] = $this->AdminLogModel
            ->alias('p1')
            ->field('p1.*,p2.username,p3.title')
            ->join('LEFT JOIN __ADMIN_USER__ as p2 on p1.admin_id=p2.id')
            ->join('LEFT JOIN __ADMIN_AUTH__ as p3 on p1.route=p3.url')
            ->limit($Page->firstRow.','.$Page->listRows)
            ->order('id desc')
            ->where($param)
            ->select();

        return $list;
    }

    /*
     * 获取单条数据
     * */
    public function getAdminLogOne($id){
        $logs = $this->AdminLogModel
            ->alias('p1')
            ->field('p1.*,p2.username,p3.title')
            ->join('LEFT JOIN __ADMIN_USER__ as p2 on p1.admin_id=p2.id')
            ->join('LEFT JOIN __ADMIN_AUTH__ as p3 on p1.route=p3.url')
            ->where(array('p1.id'=>$id))
            ->find();
        return $logs;
    }

}